{% extends "base.html" %}

{% block content %}
<div class="row">
    <div class="col">
        <div class="d-flex justify-content-between align-items-center mb-4">
            <h1><i class="bi bi-bar-chart"></i> 数据分析</h1>
            <a href="{{ url_for('index') }}" class="btn btn-secondary">
                <i class="bi bi-arrow-left"></i> 返回首页
            </a>
        </div>
        
        <div class="row">
            <div class="col-md-6">
                <div class="card mb-4">
                    <div class="card-body">
                        <h5 class="card-title">总体统计</h5>
                        <p class="card-text">总共记录错题: <span class="badge bg-primary">{{ total_mistakes }}</span> 道</p>
                    </div>
                </div>
            </div>
            
            {% if exam_type_stats %}
            <div class="col-md-6">
                <div class="card mb-4">
                    <div class="card-body">
                        <h5 class="card-title">考试类型分布</h5>
                        <canvas id="examTypeChart"></canvas>
                    </div>
                </div>
            </div>
            {% endif %}
        </div>
        
        {% if knowledge_stats %}
        <div class="card mb-4">
            <div class="card-body">
                <h5 class="card-title">知识点错误统计</h5>
                <div class="table-responsive">
                    <table class="table table-striped table-hover">
                        <thead>
                            <tr>
                                <th>知识点</th>
                                <th>错误次数</th>
                                <th>占比</th>
                            </tr>
                        </thead>
                        <tbody>
                            {% for stat in knowledge_stats[:10] %}
                            <tr>
                                <td>{{ stat.name }}</td>
                                <td>{{ stat.count }}</td>
                                <td>{{ stat.percentage }}%</td>
                            </tr>
                            {% endfor %}
                        </tbody>
                    </table>
                </div>
                <canvas id="knowledgeChart"></canvas>
            </div>
        </div>
        {% endif %}
    </div>
</div>
{% endblock %}

{% block scripts %}
<script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
<script>
    // 知识点统计图表
    const knowledgeCtx = document.getElementById('knowledgeChart');
    if (knowledgeCtx) {
        const knowledgeData = {{ knowledge_stats[:10] | tojson | safe }};
        if (knowledgeData && knowledgeData.length > 0) {
            new Chart(knowledgeCtx, {
                type: 'bar',
                data: {
                    labels: knowledgeData.map(item => item.name),
                    datasets: [{
                        label: '错误次数',
                        data: knowledgeData.map(item => item.count),
                        backgroundColor: 'rgba(54, 162, 235, 0.2)',
                        borderColor: 'rgba(54, 162, 235, 1)',
                        borderWidth: 1
                    }]
                },
                options: {
                    scales: {
                        y: {
                            beginAtZero: true
                        }
                    }
                }
            });
        }
    }
    
    // 考试类型统计图表
    const examTypeCtx = document.getElementById('examTypeChart');
    if (examTypeCtx) {
        const examTypeData = {{ exam_type_stats | tojson | safe }};
        if (examTypeData) {
            const labels = Object.keys(examTypeData);
            const data = Object.values(examTypeData);
            
            if (labels.length > 0 && data.length > 0) {
                new Chart(examTypeCtx, {
                    type: 'pie',
                    data: {
                        labels: labels,
                        datasets: [{
                            data: data,
                            backgroundColor: [
                                'rgba(255, 99, 132, 0.2)',
                                'rgba(54, 162, 235, 0.2)',
                                'rgba(255, 205, 86, 0.2)',
                                'rgba(75, 192, 192, 0.2)',
                                'rgba(153, 102, 255, 0.2)',
                                'rgba(255, 159, 64, 0.2)'
                            ],
                            borderColor: [
                                'rgb(255, 99, 132)',
                                'rgb(54, 162, 235)',
                                'rgb(255, 205, 86)',
                                'rgb(75, 192, 192)',
                                'rgb(153, 102, 255)',
                                'rgb(255, 159, 64)'
                            ],
                            borderWidth: 1
                        }]
                    }
                });
            }
        }
    }
</script>
{% endblock %}